package com.qf.flash.ui.mine;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.EaseConstant;
import com.qf.dblib.dao.entity.ChatUserBean;
import com.qf.flash.R;
import com.qf.flash.http.response.GameDetailResponse;
import com.qf.flash.mvp.contract.ContractListContrat;
import com.qf.flash.mvp.presenter.ContatListPresenter;
import com.qf.flash.ui.mine.adapter.InviteFriendItem;
import com.qf.flashlib.adapter.RVAdapter;
import com.qf.flashlib.app.FlashLib;
import com.qf.flashlib.widget.dialog.BaseDialog;
import com.qf.flashlib.widget.dialog.ViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建日期：2020/3/15 on 14:01
 * 描述:
 * 邀請好友 dialog
 */
public class InviteFriendFragment extends BaseDialog implements View.OnClickListener, ContractListContrat.View {
    RecyclerView mRecyclerView;
    private RVAdapter<Object> mAdapter;
    private ArrayList<GameDetailResponse.UsersBean> users;
    private ContatListPresenter contatListPresenter;
    int page = 1, pageSize = 30;
    private String imgUrl;
    private String orderId;
    private GameDetailResponse response;

    public static InviteFriendFragment newInstance(GameDetailResponse response) {
        InviteFriendFragment fragment = new InviteFriendFragment();
        Bundle b = new Bundle();
        b.putSerializable("data", response);
        fragment.setArguments(b);
        return fragment;
    }

    @Override
    public int setUpLayoutId() {
        return R.layout.fragment_add_friend;
    }

    @Override
    public void convertView(ViewHolder holder, BaseDialog dialog) {
        Bundle arguments = getArguments();
        if (arguments == null) {
            return;
        }
        response = (GameDetailResponse) arguments.getSerializable("data");
        if (response == null)
            return;
        imgUrl = response.getPlayImage();
        orderId = response.getId();
        contatListPresenter = new ContatListPresenter();
        contatListPresenter.attachView(this);
        contatListPresenter.getFriendList(orderId,page, pageSize);
        mRecyclerView = holder.getView(R.id.mRecyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter = new RVAdapter<>(new InviteFriendItem());
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemChildClickListener(new RVAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(View view, int position) {
                mAdapter.setSingleChosenPosition(position);
                mAdapter.notifyDataSetChanged();
            }
        });


        holder.getView(R.id.tv_sure).setOnClickListener(this);
        holder.getView(R.id.tv_cancel).setOnClickListener(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        contatListPresenter.detachView();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.tv_sure:
                int singleChosenPosition = mAdapter.getSingleChosenPosition();
                if (singleChosenPosition == -1) {
                    FlashLib.showToast("请选择好友");
                    return;
                }
                ChatUserBean bean = (ChatUserBean) mAdapter.getData(singleChosenPosition);
                String content = "快来和我一起参加游戏吧! \n队伍名称: %s; \n队伍编号: %s";
                EMMessage message = EMMessage.createTxtSendMessage(String.format(content, response.getTeamName(), response.getOrderNumber()), bean.getUid());
                // 增加自己特定的属性
                message.setAttribute(EaseConstant.CHAT_INVITE_PICTURE, imgUrl);
                message.setAttribute(EaseConstant.CHAT_TYPE_INVITE, true);
                message.setAttribute(EaseConstant.CHAT_TYPE_INVITE_NAME, response.getTeamName());
                message.setAttribute(EaseConstant.CHAT_TYPE_INVITE_NO, response.getOrderNumber());
                EMClient.getInstance().chatManager().sendMessage(message);
                FlashLib.showToast("已发送邀请");
                dismiss();
                break;
            default:
                break;
        }
    }

    @Override
    public void setUi(List<ChatUserBean> chatUserBeans) {
        List<Object> datas = mAdapter.getDatas();
        datas.addAll(chatUserBeans);
        mAdapter.notifyDataSetChanged();
    }
}
